PROJECT = "httpdemo"
VERSION = "1.0.0"

-- !字体设置：font_opposansm12_chinese
-- sys库是标配
_G.sys = require("sys")
--[[特别注意, 使用http库需要下列语句]]
_G.sysplus = require("sysplus")

---需要在云编译时添加SPI协议
spi_lcd = spi.deviceSetup(2, 7, 0, 0, 8, 40000000, spi.MSB, 1, 1) -- https://wiki.luatos.com/api/spi.html
-- spi.setup(id, cs, CPHA, CPOL, dataw, bandrate, bitdict, ms, mode)

lcd.init("st7789", {
    port = "device",
    pin_dc = 6,
    pin_pwr = 11,
    pin_rst = 10,
    direction = 0,
    w = 240,
    h = 320,
    xoffset = 0,
    yoffset = 0
}, spi_lcd)
-- 如果显示颜色相反，关闭反色
lcd.invoff()

sys.taskInit(function()

    sys.wait(1000)
    log.info(wlan.init())
    wlan.setMode(wlan.STATION)
    wlan.connect("ESP32", "776654321")

    ---等待wifi连接
    while not wlan.ready() do
        log.info("Disconnection")
        sys.wait(500)
    end
    log.info("ready !!")

    lcd.setFont(lcd.font_opposansm12_chinese) -- 具体取值可参考api文档的常量表
    while 1 do

        ---1.http连接,code为响应代码,body是返回值
        local code, headers, body = http.request("GET",
                                                 "http://quan.suning.com/getSysTime.do")
                                        .wait() -- 留意末尾的.wait()

        ---2.对body进行string直接操作
        -- 返回的是API_time_table是 {"sysTime2":"2023-01-08 12:06:09","sysTime1":"20230108120609"}
        -- print(body:sub(14,32))

        ---3.对时间进行裁剪操作，打印详细细节
        API_time = body:sub(14, 32) -- API_time = 2023-01-08 12:06:09
        API_time_year = API_time:sub(1, 4) -- 2023
        API_time_month = API_time:sub(6, 7) -- 01
        API_time_date = API_time:sub(9, 10) -- 08
        API_time_hour = API_time:sub(12, 13) -- 12
        API_time_minute = API_time:sub(15, 16) -- 06
        API_time_second = API_time:sub(18, 19) -- 09

        lcd.clear()--!在显示前先进行清屏操作
        log.info(code, "API_time:", API_time) -- !body的类型时string,time.sysTime2的类型为string
        lcd.drawStr(15 * 2, 30 * 1, "时间: " .. API_time)
        lcd.drawStr(15 * 2, 30 * 2, "年: " .. API_time_year) -- !使用汉字必须使固件中有汉字的库
        lcd.drawStr(15 * 2, 30 * 3, "月: " .. API_time_month)
        lcd.drawStr(15 * 2, 30 * 4, "日: " .. API_time_date)
        lcd.drawStr(15 * 2, 30 * 5, "时: " .. API_time_hour)
        lcd.drawStr(15 * 2, 30 * 6, "分: " .. API_time_minute)
        lcd.drawStr(15 * 2, 30 * 7, "秒: " .. API_time_second)
        sys.wait(500)

    end

end)

-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!
